﻿using AutoMapper;
using MediatR;
using Medical.Api.Write.Application.Command.Wms.WmsStorageCommand;
using Medical.Api.Write.Application.Handler.RecyclingCleaning;
using Medical.Domain.Enum;
using Medical.Domain.RecoveryCleaning;
using Medical.Domain.Wms;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using System.Reflection.Emit;

namespace Medical.Api.Write.Application.Handler.Wms.WmsStorageHandler
{
    /// <summary>
    /// 新增入库实现
    /// </summary>
    public class AddWmsStorageCommandHandlter : IRequestHandler<AddWmsStorageCommand, ApiResult<int>>
    {

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<AddWmsStorageCommandHandlter> logger;
        /// <summary>
        /// automapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 回收登记仓储
        /// </summary>
        private readonly IBaseRepository<WmsStorage> wmsStorageRep;
        /// <summary>
        /// 入库明细仓储
        /// </summary>
        private readonly IBaseRepository<WmsStorageDetail> wmsStorageDetailRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mapper">automapper</param>
        /// <param name="wmsStorage">入库仓储</param>
        public AddWmsStorageCommandHandlter(ILogger<AddWmsStorageCommandHandlter> logger, IMapper mapper, IBaseRepository<WmsStorage> wmsStorageRep, IBaseRepository<WmsStorageDetail> wmsStorageDetailRep)
        {
            this.logger = logger;
            this.mapper = mapper;
            this.wmsStorageRep = wmsStorageRep;
            this.wmsStorageDetailRep = wmsStorageDetailRep;
        }

        /// <summary>
        /// 添加入库命令处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(AddWmsStorageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 入库主表映射   
                var wmsStorage = mapper.Map<WmsStorage>(request);
                wmsStorage.CreateTime = DateTime.Now;
                wmsStorage.StorageCode = "RK" + DateTime.Now.ToString("yyMMddHHmmss");
                wmsStorage.Warehouse = request.Warehouse;
                wmsStorage.Supplier = request.Supplier;
                wmsStorage.StorageExplain = request.StorageExplain;
                wmsStorage.AuditState = false;
                wmsStorage.AuditName = request.AuditName;
                wmsStorage.AuditDate = null;
                // 插入主表
                var result = wmsStorageRep.Create(wmsStorage);
                var aaa = request.StorageDetails;
                if (result > 0 && request.StorageDetails != null && request.StorageDetails.Count > 0)
                {
                    // 循环映射明细表并赋值主表Id
                    var details = request.StorageDetails.Select(detailDto =>
                    {
                        var detail = mapper.Map<WmsStorageDetail>(detailDto);
                        detail.StorageId = wmsStorage.Id;
                        return detail;
                    }).ToList();
                    // 批量插入明细表
                    wmsStorageDetailRep.CreateRange(details);
                }
                if (result > 0)
                {
                    return Task.FromResult(new ApiResult<int>
                    {
                        Code = ApiEnums.Success,
                        Data = wmsStorage.Id,
                        Msg = "入库登记成功"
                    });
                }
                else
                {
                    return Task.FromResult(new ApiResult<int>
                    {
                        Code = ApiEnums.Fail,
                        Data = 0,
                        Msg = "入库登记失败"
                    });
                }
            }
            catch (Exception ex)
            {
                logger.LogError("入库登记异常处理", ex.Message);
                throw;
            }
        }


    }
}
